{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "608ed768-edf4-4a2e-b106-88d5f78af025",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9df62a33-4b98-4fe1-99c3-0af010eebc1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = torch.tensor([1.1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2f9eed85-3185-4752-8912-8188a566a662",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f975b05f-9073-41c3-be85-77d329f68683",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = torch.LongTensor([1,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c03d006a-4235-41b9-a29a-b0ef2f3bacb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "np_array = np.arange(20).reshape(4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "25a78754-4397-409d-be1d-7538139f8203",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = torch.from_numpy(np_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "51e3389f-adb8-4019-a5dd-464f585e41e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3,  4],\n",
       "        [ 5,  6,  7,  8,  9],\n",
       "        [10, 11, 12, 13, 14],\n",
       "        [15, 16, 17, 18, 19]], dtype=torch.int32)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a258b9e9-9c22-4d57-8de3-bf27ba6d1fdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = torch.tensor([1,2,3],dtype = torch.float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a6b17cbf-1714-442b-bfea-71b35cb8e356",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "f52a7ee2-4b3e-4d1e-81d2-a9de31959a62",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = torch.randn(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "b7462688-6aaa-48e1-a44a-1b8788b42729",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.7926, 0.6830, 0.9941],\n",
       "        [0.4025, 0.6253, 0.0763]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.rand(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e730fa1f-70aa-417d-9b50-74aaee9eb92f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "0148bfbc-75c6-464e-a598-d9dd01bacf22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 0.],\n",
       "        [0., 0., 0.]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "1bf60d80-9de2-47bf-a4f8-e87299c4f233",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 0.],\n",
       "        [0., 0., 0.]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros_like(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "e1cd3069-1642-4f12-b8c9-8450f55dc8fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [1., 1., 1.]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "58c16489-9af5-4428-be62-ffed97b818b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [1., 1., 1.]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones_like(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "e010ad47-e057-40b4-be5e-fe4100aa20f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.size(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "32e90081-8ce7-4e9b-8cfc-05ed88ba3e01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.6971, 0.9293, 0.1742],\n",
       "        [0.1263, 0.9971, 0.2100]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.rand_like(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "ba807b36-607a-4140-9db5-0f0cd36f9c61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "device(type='cpu')"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "36b6cb59-a6ad-4ad0-bb54-def3f80e96c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.cuda.is_available()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "30222d06-2058-4509-8cf4-8b0a2004dc16",
   "metadata": {},
   "outputs": [],
   "source": [
    "if torch.cuda.is_available():\n",
    "    t = t.to('cuda')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "487928f0-512e-4aba-bb22-bc3c7404f004",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = torch.tensor([1,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "69e269b8-4473-4f11-8bd8-8b7d1e3d7237",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 2.])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = t.type(torch.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "7da7bba8-170f-41c4-a35b-9e98ad2fd564",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.long()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "05307c09-da3a-4532-97ec-0aa1cbd11083",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "365cdc7b-2a16-4fdf-b89f-3869cd69ad4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([4, 5])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t + 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "ab37d10a-8fbb-4fad-bcfc-08e2f75f611b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "5c690b2d-45df-41a9-9b21-8169fe3861b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2, 4])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "644f8645-cd6e-4e89-b460-f5dc3f0753e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([4, 8])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.add_(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "88e6bfe8-4ec0-4189-b0d1-be4f811eca7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(12)"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "f3cc5343-a586-4191-8d33-3fa22c09b005",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = torch.rand(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "af8092b8-f4b9-48ca-8834-f8d4942ba1f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.7656)"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "66c5c162-02f6-4fd3-a209-b38542f35200",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.8982],\n",
       "        [0.6330]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转置\n",
    "t.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "fcb5dc0c-176d-49b5-9757-34a741c711c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# tensor 转成python类型\n",
    "pt = t.sum().item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "16e00618-1192-4480-8fc4-aba75bf268fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(pt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "9cadfbdf-f4d8-43a1-a187-7ce2f91ea877",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.898184  , 0.63300675]], dtype=float32)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "1247b332-931b-4dec-b3a8-b6512d24bc1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "0c89f0c7-10b2-4fce-90a6-317cb55e5358",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Tensor"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "35c3a61d-6eed-4cf2-9567-8cdbc2399f5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.8982],\n",
       "        [0.6330]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.view(2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "7779001e-81a3-4bf4-aa47-6b19046c0217",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.8982],\n",
       "        [0.6330]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.view(-1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "2025709f-b916-42e4-b392-151d8efc6b71",
   "metadata": {},
   "outputs": [],
   "source": [
    "t1 = torch.randn(1,2,3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "7d8e6966-a714-4c9b-ad6f-878f70078204",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-0.8111, -0.0961,  0.7834,  0.8375],\n",
       "         [ 1.6639,  1.4006, -1.1553, -1.7740],\n",
       "         [-0.7074, -0.3895, -0.7301,  0.8333]],\n",
       "\n",
       "        [[-0.1770,  0.0117,  0.9428,  1.5132],\n",
       "         [-0.7978, -1.2081, -0.1625,  0.3762],\n",
       "         [ 0.3643, -1.5488, -1.1379,  0.0777]]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将外围的维度为1的去掉\n",
    "torch.squeeze(t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17a3bdf0-fce8-4ef3-ab1f-31b72ad3e75c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
